import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class Coins {

	public  Image img;
	public  int x,y;
	public  int type;
	public int width;
	public int height;
	public int currFrame = 0;
	public int currBox = 0;
	public int ROW;
	public int tick;
	public  String path;
	
	public static final int BLOOD = 0;
	public static final int VCOIN = 1;
	public static final int BOX = 2;
	public static final int DIAMOND = 3;
	
	public int mdiamond ;

	
	public  boolean openBox = false;
	
	
	public static Vector vectorDiamond = new Vector();
	
	
	public Coins(int type1, int x1, int y1)
	{

		
		x = x1;
		y = y1;		
		type = type1;
		init(type1);
		width = img.getWidth();
		switch (type) {
		case VCOIN:
			ROW = 4;
			height = img.getHeight()/ROW;
			break;
		case BLOOD:			
			height = img.getHeight();
			break;		
		case BOX :
			ROW = 2;
			height = img.getHeight()/ROW;
			break;
		case DIAMOND :
			height = img.getHeight();
			break;

		default:
			break;
		}	
		
	}
	
	public  void init(int type1)
	{
		switch (type1) {
		case VCOIN:
			path = "/tien.png";
			break;
		case BLOOD:
			path = "/mau.png";
			break;		
		case BOX :
			path = "/box.png";
			break;
		case DIAMOND:
			path = "/diamond.png";
			break;

		default:
			break;
		}
		
		try {
			img = Image.createImage(path);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public boolean collison(int x2, int y2, int w2, int h2)
	{
//		if( ((x2 + w2/2 >= x && x2 + w2/2 <= x + width) || (x2 <= x + width && x2 >= x) || (x2 + w2 >= x && x2
//				+ w2 <= x + width)) && ((y2>=y && y2 <= y+height) ||( y2+ h2>=y && y2+h2<=y+height) ))
		if(x+width/2>= x2 && x+width/2<=x2+w2  && ((y2>=y && y2 <= y+height) ||( y2+ h2>=y && y2+h2<=y+height) ))
		{			
			return true;
		}
		return false;
	}
	

	
	public void update()
	{
		tick++;
		if(tick % 10 == 0)
		{
			currFrame = ++currFrame % ROW;
		}		
	}
	
	
	public  void paint(Graphics g)
	{
			
		switch (type) {
		
		case VCOIN:
			update();
			g.drawRegion(img, 0, height*currFrame, width, height, 0, x, y, Graphics.VCENTER|Graphics.HCENTER);
			break;
			
		case BLOOD:
			g.drawImage(img, x, y,  Graphics.HCENTER|Graphics.VCENTER);
			break;		
			
		case BOX:	
			if(openBox)
			{
				currBox = 1;
			}
			else
			{
				currBox = 0;
			}
			g.drawRegion(img, 0, height*currBox, width, height, 0, x, y, Graphics.VCENTER|Graphics.HCENTER);						
			break;
			
		default:
			break;
		}		
	}
}
